package com.softwinner.awbt.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.softwinner.awbt.model.BTDeviceInfo;

import java.util.List;

/**
 * Created by xiasj on 17-12-20.
 */

public class DeviceAdapter extends BaseAdapter {

    private List<BTDeviceInfo> mDeviceInfos;
    private Context mContext;

    public DeviceAdapter(Context context, List<BTDeviceInfo> deviceInfos) {
        mDeviceInfos = deviceInfos;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mDeviceInfos.size();
    }

    @Override
    public BTDeviceInfo getItem(int i) {
        return mDeviceInfos.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(android.R.layout
                    .simple_list_item_2, null, false);
            viewHolder = new ViewHolder();
            viewHolder.name = (TextView) convertView.findViewById(android.R.id.text1);
            viewHolder.address = (TextView) convertView.findViewById(android.R.id.text2);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        BTDeviceInfo deviceInfo = mDeviceInfos.get(i);
        if (TextUtils.isEmpty(deviceInfo.getName())) {
            viewHolder.name.setText(android.R.string.unknownName);
        } else {
            viewHolder.name.setText(deviceInfo.getName());
        }

        viewHolder.address.setText(deviceInfo.getAddress());
        return convertView;
    }

    class ViewHolder {
        public TextView name;
        public TextView address;
    }
}
